/**
 * 部门js
 */
var transaction = {
	curPage : 0,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				transaction.curPage = 1;
				transaction.setPageChange(null);
				transaction.search();
			}
		};
		
	},
	getResource :  function() {
		var url = 'mgr/ProductInfoCtrl/getProductInfoList';
		var params = {
			'curPage' : 1,
			'pageSize' : 9999,
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var option = '<option value="">请选择产品名称</option>';
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					option = option + '<option value="' + singleObj.id + '">'
							+ singleObj.name + '</option>';
				}
				$('select[name="productId"]').append(option);
			}else{
				alert(data.msg);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			transaction.curPage = 1;
			transaction.setPageChange(null);
			transaction.search();
		});

		$('#addData').on('click', function() {
			var path = APP_ADDRESS + 'mgr/transaction/bespeak/bespeakAdd.jsp';
			art.dialog.open(path, {
				title : '添加预约',
				width : 500,
				height : 200,
				ok : function() {
					var returnStatus = this.iframe.contentWindow.submitForm();
					if (returnStatus) {
						transaction.setPageChange(null);
						transaction.search();
					}
					return false;
				},
				cancel : true
			});
		});
		
		$('#collectionData').on('click', function() {
			var path = APP_ADDRESS + 'mgr/transaction/bespeak/bespeakCollection.jsp';
			art.dialog.open(path, {
				title : '预约统计',
				width : 500,
				height : 500,
				ok : function() {
					var returnStatus = this.iframe.contentWindow.submitForm();
					if (returnStatus) {
						transaction.setPageChange(null);
						transaction.search();
					}
					return false;
				},
				cancel : true
			});
		});
		
		$('#excel').on('click', function() { 
			var url = '../../../mgr/reservation/excel';
			if (typeof (pageIdx) != "undefined") {
				transaction.curPage = pageIdx;
			}

			var id = $.trim($('#searchModForm input[name="id"]').val());
			var userName = $.trim($('#searchModForm input[name="userName"]').val());
			var productId = $.trim($('#productId').val());
			var beginDate = $.trim($('#searchModForm input[name="begin_date"]').val());
			var endDate = $.trim($('#searchModForm input[name="end_date"]').val());
			
			location.href = url+"?id="+ id + "&userName=" + userName + "&productId=" + productId
							+"&beginDateStr=" + beginDate + "&endDateStr=" + endDate
							+"&userId=" + userId;   
		});
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/reservation/getSize';
	
		if (typeof (pageIdx) != "undefined") {
			transaction.curPage = pageIdx;
		}

		var id = $.trim($('#searchModForm input[name="id"]').val());
		var userName = $.trim($('input[name="userName"]').val());
		var productId = $.trim($('#productId').val());
		var beginDate = $.trim($('input[name="begin_date"]').val());
		var endDate = $.trim($('input[name="end_date"]').val());
		
		var params = {
			'id' : id,
			'userName' : userName,
			'productId' : productId,
			'financial' : userId,
			'userId':userId,
			'beginDate':beginDate,
			'endDate':endDate,
		};

		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.result;
				var pageTotal = (totalCount / transaction.pageSize > 0) ? Math
						.ceil(totalCount / transaction.pageSize) : 1;
				pageHelper.init(pageTotal, totalCount, transaction.search,
						delCurPage); // 翻页事件
			}else{
				alert(data.msg);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询交易记录
	search : function(pageIdx) {
		var url = 'mgr/reservation/list';
		if (typeof (pageIdx) != "undefined") {
			transaction.curPage = pageIdx;
		}

		var id = $.trim($('#searchModForm input[name="id"]').val());
		var userName = $.trim($('input[name="userName"]').val());
		var productId = $.trim($('#productId').val());
		var beginDate = $.trim($('input[name="begin_date"]').val());
		var endDate = $.trim($('input[name="end_date"]').val());
		var params = {
			'id' : id,
			'userName' : userName,
			'productId' : productId,
			'financial' : userId,
			'userId':userId,
			'beginDate':beginDate,
			'endDate':endDate,
			'curPage' : transaction.curPage,
			'pageSize' : transaction.pageSize
		};
		
		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';
			if (data.result != null && data.result.length > 0) {
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					
					// 更改订单创建 的日期格式

					var orderCreatedOn = getSmpFormatDateByLong(singleObj.cts,
							true);
					
					
					localStorage.setItem(singleObj.col01id + "_Bespeak_Key", JSON.stringify(singleObj));

					var status = getBespeakStatus(singleObj.status);

					tr += '<tr><td><input type="hidden" name="id" value="'
							+ singleObj.col01id
							+ '"/>'
							+ '<input type="hidden" name="productName" value="' 
							+ singleObj.col10productId
							+ '"/>'
							+ (i + 1)
							+ '</td><td>'
							+ singleObj.col01id
							+ '</td><td>'
							+ (singleObj.col06userName != null?singleObj.col06userName:"")
							+ '</td><td>'
							+ (singleObj.col09userPhoneNum != null?singleObj.col09userPhoneNum:"")
							+ '</td><td>'
							+ singleObj.col02amount
							+ '</td><td>'
							+ singleObj.col03payAmount
							+ '</td><td>'
							+ singleObj.col04status
							+ '</td><td>'
							+ singleObj.col12productName
							+ '</td><td>'
							+ singleObj.col14productCompanyName
							+ '</td><td>'
							+ (singleObj.col16insideName != null?singleObj.col16insideName:"") 
							+ '</td><td>'
							+ (singleObj.col17insideDepName != null?singleObj.col17insideDepName:"")
							+ '</td><td>'
							+ singleObj.col19cts
							+ '</td><td>'
							+ '<a type="button" name="bespeak_94" class="btn btn-primary btn-xs lookData"  style="margin-left: 5px; margin-top: 5px; display:none;">'
							+ '<i class="fa fa-reorder"></i> 查看 </a>'
							+ '<a type="button" name="bespeak_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px; margin-top: 5px; display:none;">'
							+ '<i class="fa fa-edit"></i> 修改</a>'
							+ '</td></tr>';
				}
			} else {
				tr = '<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			transaction.bindEventOtherBtn();
			jsUtils.controlFunctionBtn();
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {
		$('.deleteData').unbind();
		$('.deleteData').on(
				'click',
				function() {
					var thisObj = $(this);
					art.dialog.confirm('你确认删除？', function() {
						var id = thisObj.parent().parent().find(
								'input[name="id"]').eq(0).val();
						var url = 'mgr/transactionCtrl/deletetransaction';
						var params = {
							'id' : id
						};
						var cb = function(data) {
							alert('删除成功！');
							transaction.setPageChange(transaction.curPage);
							transaction.search(transaction.curPage);
						}
						sendApiNoLoad(url, params, cb, 'POST');
					}, function() {
					});
				});
		$('.updateData').unbind();
		$('.updateData').on(
				'click',
				function() {
					var thisObj = $(this);
					
					var id = thisObj.parent().parent().find('input[name="id"]').eq(0).val();
					 
					art.dialog.data('id', id);
					 
					var path = APP_ADDRESS
							+ 'mgr/transaction/bespeak/bespeakUpdate.jsp';
					art.dialog.open(path, {
						title : '更改预约记录',
						width : 450,
						height : 200,
						cancel : true,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								transaction.setPageChange(transaction.curPage);
								transaction.search(transaction.curPage);
							}
							return false;
						},
						cancel : true
					});
				});
		$('.lookData').unbind();
		$('.lookData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					
					art.dialog.data('id', id);
					var path = APP_ADDRESS
							+ 'mgr/transaction/bespeak/bespeakLook.jsp';
					art.dialog.open(path, {
						title : '查看预约记录',
						width : 450,
						height : 250,
						cancel : true
					});
				});
	},

	init : function() {
		jsUtils.controlFunctionBtn();
		transaction.getResource();
		transaction.setPageChange(null);
		transaction.search();
		transaction.startEvent();
		transaction.bindEvent();
	}
};

$(document).ready(function() {
	transaction.init();
});

//提供给外部刷新调用
function refashData() {
	transaction.setPageChange(null);
	transaction.search();
}
